<?php

class modelField
{
	public $name;
	public $type;
	public $length;
	public $decimals;
	public $zerofill = false;
	public $null = false;
	public $primaryKey = false;
	public $default;
	public $autoIncrement = false;
	public $enumValues;
	public $localized = false;
		
	public function __construct($description)
	{		
		$this->name = $description['Field'];
		
		$typePattern = '/^([a-z]*)\(*([a-zA-Z0-9\'\,]+)*\)* *([a-z ]+)*$/';
		preg_match($typePattern, $description['Type'], $typeMatches);

		$this->type = $typeMatches[1];

		$this->zerofill = strstr($typeMatches[3], 'zerofill') ? true : false;
		$this->unsigned = strstr($typeMatches[3], 'unsigned') ? true : false;
		$this->autoIncrement = strstr($description['Extra'], 'auto_increment') ? true : false;
		
		$this->primaryKey = strstr($description['Key'], 'PRI') ? true : false;
		
		$this->default = $description['Default'];
		
		switch($this->type)
		{
			case 'tinyint':
			case 'smallint':
			case 'mediumint':
			case 'integer':
			case 'int':
			case 'bigint':
				$this->length = $typeMatches[2];
				break;
				
			case 'float':
			case 'double':
			case 'real':
			case 'numeric':
				list($this->length, $this->decimals) = explode(',', $typeMatches[2]);
				break;
			
			case 'varchar':
				$this->length = $typeMatches[2];
				break;
				
			case 'enum':
				$this->enumValues = explode(',', $typeMatches[2]);
				foreach ($this->enumValues as &$value)
				{
					$value = trim($value, '\''); //bug: can the enum value start with a single quote?
				}
				break;
		}
	}
}

?>